package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.vo.AdminDaoEcologyPointsVO;
import com.sc.nft.enums.DaoEcologyPointsDetailAmountTypeEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.DaoEcologyPoints;
import com.sc.nft.mapper.DaoEcologyPointsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * DAO-生态燃料余额表(sc_dao_ecology_points)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-12-06 18:45:26
 */
@Slf4j
@Repository
public class DaoEcologyPointsDao extends ServiceImpl<DaoEcologyPointsMapper, DaoEcologyPoints> {

    public DaoEcologyPoints getByDaoId(Long daoId) {
        LambdaQueryWrapper<DaoEcologyPoints> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DaoEcologyPoints::getDaoId, daoId);
        return getOne(wrapper);
    }

    public boolean subAmount(Long daoId, BigDecimal transferAmount) {
        return baseMapper.subAmount(daoId, transferAmount);
    }

    public boolean addFrozenAmountByDaoId(Long daoId, BigDecimal amount, DaoEcologyPointsDetailAmountTypeEnum amountType) {
        LambdaUpdateWrapper<DaoEcologyPoints> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DaoEcologyPoints::getDaoId, daoId);
        if (DaoEcologyPointsDetailAmountTypeEnum.RELEASE_DAO == amountType){
            wrapper.setSql("dao_frozen_amount = dao_frozen_amount + " + amount + ",sum_amount = sum_amount + " + amount);
        }else if (DaoEcologyPointsDetailAmountTypeEnum.RELEASE_RESONANCE_SERVICE_CENTER == amountType){
            wrapper.setSql("resonance_service_center_frozen_amount = resonance_service_center_frozen_amount + " + amount + ",sum_amount = sum_amount + " + amount);
        }else {
            wrapper.setSql("frozen_amount = frozen_amount + " + amount + ",sum_amount = sum_amount + " + amount);
        }

        return update(wrapper);
    }

    public boolean subFrozenAmountByDaoId(Long daoId, BigDecimal amount, DaoEcologyPointsDetailAmountTypeEnum amountType) {
        LambdaUpdateWrapper<DaoEcologyPoints> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DaoEcologyPoints::getDaoId, daoId);
        if (DaoEcologyPointsDetailAmountTypeEnum.RELEASE_DAO == amountType){
            wrapper.setSql("dao_frozen_amount = dao_frozen_amount - " + amount + ",sum_amount = sum_amount - " + amount);
        }else if (DaoEcologyPointsDetailAmountTypeEnum.RELEASE_RESONANCE_SERVICE_CENTER == amountType){
            wrapper.setSql("resonance_service_center_frozen_amount = resonance_service_center_frozen_amount - " + amount + ",sum_amount = sum_amount - " + amount);
        }else {
            wrapper.setSql("frozen_amount = frozen_amount - " + amount + ",sum_amount = sum_amount - " + amount);
        }
        return update(wrapper);
    }

    public boolean addAmountAndSubFrozenAmount(Long daoId, BigDecimal amount) {
        return baseMapper.addAmountAndSubFrozenAmount(daoId, amount);
    }

    public boolean addAmount(Long daoId, BigDecimal amount) {
        LambdaUpdateWrapper<DaoEcologyPoints> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DaoEcologyPoints::getDaoId, daoId);
        wrapper.setSql("sum_amount = sum_amount+" + amount + " , amount = amount + " + amount);
        return update(wrapper);
    }

    public Page<AdminDaoEcologyPointsVO> getPage(String daoName, Page page) {
        return baseMapper.getPage(page, daoName);
    }

    public boolean subAllAmount(Long daoId, BigDecimal amount) {
        LambdaUpdateWrapper<DaoEcologyPoints> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DaoEcologyPoints::getDaoId, daoId);
        wrapper.setSql("amount = amount - " + amount);
        return update(wrapper);
    }
}